package com.ruoyi.common.utils;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;

import javax.xml.bind.TypeConstraintException;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author : hcl
 * @Date : 2023/8/7
 */
public class CollectionUtils extends CollectionUtil {

    public static Set<Long> parseLongSet(String str) {
        String[] array = tokenizer(str);
        if (array == null) {
            return Collections.emptySet();
        }
        return Stream.of(array).filter(Objects::nonNull)
                .map(Long::parseLong)
                .collect(Collectors.toCollection(HashSet::new));
    }

    /**
     * @param val "1,2,3"
     */
    public static List<Long> toLongList(String val) {
        return toList(val, Long.class);
    }

    public static List<String> toStrList(String val) {
        if (StrUtils.isEmpty(val)) {
            return Collections.emptyList();
        }
        String[] vs = val.split(StrUtil.COMMA);
        List<String> array = new ArrayList<>(vs.length);
        addAll(array, vs);
        return array;
    }

    private static <T> List<T> toList(String val, Class<T> cls) {
        if (StrUtils.isEmpty(val)) {
            return Collections.emptyList();
        }
        boolean contains = cls.getName().contains("java.lang");
        if (!contains) {
            throw new TypeConstraintException("不支持的转换类型，仅支持基础类型");
        }
        String[] vs = val.split(StrUtil.COMMA);
        List<T> array = new ArrayList<>(vs.length);
        try {
            for (String v : vs) {
                array.add((T) cls.getConstructor(String.class).newInstance(v));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return array;
    }

    private static String[] tokenizer(String str) {
        if (StrUtil.isNotBlank(str)) {
            StringTokenizer st = new StringTokenizer(str, StrUtil.COMMA);
            String[] array = new String[st.countTokens()];
            int index = 0;
            while (st.hasMoreElements()) {
                array[index++] = st.nextToken();
            }
            return array;
        }
        return null;
    }

    public static <T> void add(List<T> list, T val) {
        if (Objects.nonNull(list) && Objects.nonNull(val)) {
            list.add(val);
        }
    }

    /**
     * 二分查找
     *
     * @param list 已排序的数据
     */
    public static int binarySearch(List<Long> list, Long find) {
        if (CollectionUtils.isNotEmpty(list) && find != null) {
            int l = 0, r = list.size() - 1;
            while (l <= r) {
                int mid = (r + l) / 2;
                Long val = list.get(mid);
                if (val > find) {
                    r = mid - 1;
                } else if (val < find) {
                    l = mid + 1;
                } else {
                    return mid;
                }
            }
        }
        return -1;
    }

    /**
     * @return true:同时为空
     */
    public static boolean isAllEmpty(Collection<?>... collections) {
        for (Collection<?> collection : collections) {
            if (isNotEmpty(collection)) return false;
        }
        return true;
    }
}
